package rbac.View.Main.View;

import rbac.View.Main.DoMain.Vo.BaseVo;
import rbac.View.Main.Service.IBaseService;
import rbac.View.Main.DoMain.Vo.Result;

public abstract class BaseView<Vo extends BaseVo, IService extends IBaseService> {
    IService service;

    Vo[] cache;

    static void printError(String message) {
        System.out.println("\033[31m" + message + "\033[0m");
    }

    Vo getCacheById() {
        System.out.println("请输入ID,ID必须是大于0的整数:");
        if (!View.input.hasNextInt()) {
            View.input.nextLine();
            printError("输入非法，请输入正确的ID！");
            return null;
        }

        int id = View.input.nextInt();
        View.input.nextLine();
        if (id < 1) {
            printError("输入非法，请输入正确的ID！");
            return null;
        }
        if (cache == null) {
            Result result = service.list();
            cache = (Vo[]) result.data;
        }
        Vo vo = null;
        for (Vo item : cache) {
            if (item.getId() == id) {
                System.out.println("该ID对应的数据是：");
                System.out.println(item);
                vo = item;
                break;
            }
        }
        if (vo == null) {
            printError("当前没有该ID对应的数据！！");
            return null;
        }

        return vo;
    }
}
